
var ToolBar = new Class({
    Implements: Options,
    options: {
        buttons: [
            { action: 'add',    icon: '../tree/imgs/add.png',    title: 'Add New' },
            { action: 'rename', icon: '../tree/imgs/rename.png', title: 'Rename' },
            { action: 'remove', icon: '../tree/imgs/remove.png', title: 'Delete' }
        ],
        fade: true
    },
    initialize: function(toolbar, options) {
        this.toolbar = toolbar;
        this.setOptions(options);

        this.toolbar.addEvents({
            'doSelect': this.showTools.bind(this),
            'unSelect': this.hideTools.bind(this)
        });
    },
    showTools: function(current) {

        var wrapper = current.getParent();

        var toolbar = wrapper.getElement('.cv-toolbar');
        if(!toolbar) {
            var miniButtons = [];

            this.options.buttons.each(function(item) {
                miniButtons.push(new Element('img.cv-toolbar-button.cv-toolbar-button-' + item.action + '[title=' + item.title + ']', {
                    'alt': '[' + item.title + ']',
                    'src': item.icon,
                    'events': {
                        'click': (function(e) {
                            e.stop();
                            this.toolbar.fireEvent('action', [item.action, current, this]);
                        }).bind(this)
                    }
                }));
            }, this);

            toolbar = new Element('span.cv-toolbar');
            toolbar.adopt(miniButtons);
            toolbar.fade('hide');
            wrapper.grab(toolbar);
        }
        toolbar.fade(this.options.fade ? 'in' : 'show');
    },
    hideTools: function(current) {
        var toolbar = current.getElement('.cv-toolbar');
        if(toolbar) {
            toolbar.fade(this.options.fade ? 'out' : 'hide');
        }
    }
});


